package cn.wyu.blogrest.service;

import cn.wyu.blogrest.entity.Auth;
import cn.wyu.blogrest.entity.User;
import cn.wyu.blogrest.repository.AuthRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

@Service
@Transactional
public class AuthService {

    @Autowired
    private AuthRepository authRepository;

    /**
     * 生成新的accessToken
     * @param user
     * @return
     */
    public Auth getAccessToken(User user){
        Auth auth = new Auth();

        auth.setUser(user);
        Timestamp expireTime = new Timestamp(System.currentTimeMillis()+1000L*60*60*24*30);
        auth.setExpireTime(expireTime);
        authRepository.save(auth);
        return  auth ;
    }
}
